package com.example.springbott.controller;

import com.example.springbott.aspactJ.OperationName;
import com.example.springbott.bean.MarketRole;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.BaseRespVo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.kerbal.AdminRolePermissionsVo;
import com.example.springbott.service.adminRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;


/**
 * 管理角色控制器
 *
 * @author kerbal
 * @date 2022/11/22
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {


    @Autowired
    adminRoleService adminRoleService;

    /**
     * 管理角色选择
     * http://182.92.235.201:8083/admin/role/options
     *
     * @return {@link BaseRespVo}
     */
    @RequestMapping("options")
    @OperationName("1")
    public BaseRespVo adminRoleOptions() {
        CommonData commonData = adminRoleService.adminRoleOptions();
        return BaseRespVo.ok(commonData);
    }


    /**
     * 管理角色列表
     * http://localhost:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
     *
     * @param basePageInfo 基本信息页
     * @param name         名字
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    @OperationName("1")
    public BaseRespVo adminRoleList(BasePageInfo basePageInfo, String name) {
        CommonData commonData = adminRoleService.adminRoleList(basePageInfo, name);
        return BaseRespVo.ok(commonData);
    }


    /**
     * 管理角色创建
     * http://localhost:8083/admin/role/create
     *
     * @param marketRole 市场作用
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:create")
    @RequestMapping("create")
    @OperationName("1")
    public BaseRespVo adminRoleCreate(@RequestBody MarketRole marketRole) {

        MarketRole marketRole1 = adminRoleService.adminRoleCreate(marketRole.getDesc(), marketRole.getName());
        return BaseRespVo.ok(marketRole1);
    }


    @RequiresPermissions("admin:admin:update")
    @RequestMapping("update")
    @OperationName("1")
    public BaseRespVo adminRoleUpdate(@RequestBody MarketRole marketRole) {
        Integer code = adminRoleService.adminRoleUpdate(marketRole);
        if (code == 1) {
            return BaseRespVo.ok(null);
        } else {
            return BaseRespVo.invalidData("修改失败");
        }
    }


    @RequiresPermissions("admin:role:delete")
    @RequestMapping("delete")
    @OperationName("1")
    public BaseRespVo adminRoleDelete(@RequestBody MarketRole marketRole) {
        Integer code = adminRoleService.adminRoleDelete(marketRole);
        if (code == 1) {
            return BaseRespVo.ok(null);
        } else {
            return BaseRespVo.invalidData("删除");
        }

    }


    @PostMapping("permissions")
    @OperationName("1")
    public BaseRespVo adminRolePermissionsGet(@RequestBody Map map) {
        String[] permissions = new String[((ArrayList<String>) map.get("permissions")).size()];
        ((ArrayList<String>) map.get("permissions")).toArray(permissions);
        Integer code = adminRoleService.adminRolePermissionsGet(permissions, (Integer) map.get("roleId"));
        if (code == 200) {
            return BaseRespVo.ok(null);
        } else {
            return BaseRespVo.invalidData();
        }
    }


    @RequiresPermissions("admin:role:permission:update")
    @GetMapping("permissions")
    @OperationName("1")
    public BaseRespVo adminRolePermissionsGet(Integer roleId) {

        AdminRolePermissionsVo adminRolePermissionsVo = adminRoleService.adminRolePermissions(roleId);
        return BaseRespVo.ok(adminRolePermissionsVo);

    }


}
